<?php
include "../conn.php";
include "t-cookie.php";
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/31
 * Time: 0:43
 */

$txtTaskName = $_GET["txtTaskName"];
$ddlType = $_GET["ddlType"];
$ddlSource = $_GET["ddlSource"];
$txtTaskDescription = $_GET["txtTaskDescription"];
$btnSaveTask = $_GET["btnSaveTask"];
$ProjectId = $_GET["id"];
$StudentId = $_GET["StudentId"];
$Year = $_SESSION["t-Year"];
$TeacherId = $_SESSION["t-id"];
$pid = $_POST['projectid'];
 $asstask = $_POST['asstask'];
 $draft = $_POST['draft'];


//修改数据
if ($btnSaveTask == "保存课题信息") {
    $sql = "update project set ProjectName='$txtTaskName',Category='$ddlType',ProjectOrigin='$ddlSource',Summary='$txtTaskDescription' where ProjectId=$ProjectId";
    echo "true </br>";
    if (mysqli_query($conn, $sql)) {
        header("Location:t-mykt.php");
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

//添加课题信息
if ($btnSaveTask == "添加") {
    if ($StudentId!="") {
        //判断学生是否已被添加
        $sql="select * from student WHERE StudentId=$StudentId";
        $result=mysqli_query($conn,$sql);
        if($result->num_rows>0){

            //插入课题，添加学生信息
            $sql = "select max(ProjectId) from project";
            $result = mysqli_query($conn, $sql);
            $row = mysqli_fetch_row($result);
            $ProjectId = $row[0] + 1;
            $sql = "INSERT INTO project (ProjectId,ProjectName,Year,Category,ProjectOrigin,Summary,Teacher_TeacherId,Student_StudentId,activate,time) VALUES ('$ProjectId', '$txtTaskName', '$Year', '$ddlType', '$ddlSource', '$txtTaskDescription', '$TeacherId','$StudentId',1,NULL )";
            if (mysqli_query($conn, $sql)) {
                //添加学生外键
                $sql="update student set Teacher_TeacherId=$TeacherId WHERE  StudentId=$StudentId";
                if($conn->query($sql)){
                    //添加到指导表
                    //计算guidemark最大id
                    $sql = "select max(GuideMarkId) from guidemark ";
                    $result = mysqli_query($conn, $sql);
                    $row = mysqli_fetch_row($result);
                    $GuideMarkId = $row[0] + 1;
                    echo $GuideMarkId;
                    //添加到指导表
                    $addguidemark = "insert into guidemark(GuideMarkId,Year,Order1,Order2,Order3,Order4,Order5,remark,Project_ProjectId) values
('$GuideMarkId','$Year',NULL,NULL,NULL,NULL,NULL,NULL,'$ProjectId')";
                    if (mysqli_query($conn,$addguidemark)) {?>
                        <script type="text/javascript">
                            alert("添加指导表，添加学生，添加课题成功");
                        </script>
                        <?
                        echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
//                    header("Location:t-mykt.php");
                    } else {
                        echo "添加指导表失败  </br>";
                        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                    }
                }else echo "教师外键添加失败";

            } else {?>
                <? echo "Error: " . $sql . "<br>" . mysqli_error($conn)."<br>";
                echo "课题添加失败.<br>";
            }
        }else{?>
            <script type="text/javascript">
                alert("该学生已被添加");
            </script>
<?       echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
        }





    } else {
        $sql = "select max(ProjectId) from project";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_row($result);
        $ProjectId = $row[0] + 1;
        $sql = "INSERT INTO project (ProjectId,ProjectName, Year, Category, ProjectOrigin, Summary, Teacher_TeacherId) VALUES ('$ProjectId', '$txtTaskName', '$Year', '$ddlType', '$ddlSource', '$txtTaskDescription', '$TeacherId')";
        if (mysqli_query($conn, $sql)) {
            header("Location:t-mykt.php");
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
    }
}
//删除课题信息
if ($btnSaveTask == "delete") {
    $sql = "delete from project where ProjectId=$ProjectId";
    if (mysqli_query($conn, $sql)) {
        echo "删除成功";
    } else echo "删除失败,未删除学生";
}

//添加学生选题
if ($btnSaveTask == "保存学生") {
    //先判断教师是否添加该学生
    $sqlstudent = "SELECT *  FROM student WHERE Teacher_TeacherId =$TeacherId AND StudentId =$StudentId";
    $result = mysqli_query($conn, $sqlstudent);
    if ($row = mysqli_num_rows($result) > 0) {
        //判断学生是否已经添加课题
        $sqlcheck = "select * from project where Student_StudentId=$StudentId";
        $result = mysqli_query($conn, $sqlcheck);
        //$row = mysqli_fetch_array($result);

        if (mysqli_num_rows($result) == 0) {

            //指导表判断课程是否存在
            $sqlguidecheck = "select * from guidemark  where Project_ProjectId=$ProjectId";
            $guidecheckresult = mysqli_query($conn, $sqlguidecheck);
            if (mysqli_num_rows($guidecheckresult) == 0) {
                //计算guidemark最大id
                $sql = "select max(GuideMarkId) from guidemark ";
                $result = mysqli_query($conn, $sql);
                $row = mysqli_fetch_row($result);
                $GuideMarkId = $row[0] + 1;

                //添加到指导表
                $addguidemark = "insert into guidemark(GuideMarkId,Year,Order1,Order2,Order3,Order4,Order5,remark,Project_ProjectId) values
('$GuideMarkId','$Year',NULL,NULL,NULL,NULL,NULL,NULL,'$ProjectId')";
                if ($conn->query($addguidemark)) {
                    echo "添加指导表成功 </br>";
                } else echo "添加指导表失败  </br>";
            } else "已添加到指导表";

            //课题添加学生
            $sqlproject = "update project set Student_StudentId=$StudentId,activate=1 WHERE ProjectId=$ProjectId";
            if (mysqli_query($conn, $sqlproject)) {
                ?>
                <script type="text/javascript">
                    alert("添加学生成功");
                </script>
                <?
                echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }

        } else {
            ?>
            <script type="text/javascript">
                alert("该学生已添加课题");
            </script>
            <?
            echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
        }
    } else{
        ?>
        <script type="text/javascript">
            alert("您未添加该学生");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
    }
}


//删除学生选题
if ($btnSaveTask == "删除学生") {
    $Dguidemark = "delete from  guidemark where Project_ProjectId=$ProjectId";
    if (mysqli_query($conn, $Dguidemark)) {
        echo "指导表删除成功";
    } else echo "指导表删除失败";
    $sql="update  student set Teacher_TeacherId=NULL WHERE StudentId=$StudentId";
    if(mysqli_query($conn,$sql)){
        echo "教师成功移除该学生 <br>";
    }else {
        echo "教师未移除该学生 <br>";
        echo mysql_error($conn);
    }
    $sql = "update  project set Student_StudentId=Null,activate=0 where ProjectId=$ProjectId";
    if (mysqli_query($conn, $sql)) {
        ?>
        <script type="text/javascript">
            alert("学生删除成功");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='t-mykt.php';</script>";
    } else echo "学生删除失败";

}

//上传任务书

if ($asstask != null) {
    if (!empty($_FILES['upassfile']['name'])) {
        $fileinfo = $_FILES['upassfile'];
        $type = strstr($fileinfo['name'], ".");
        if ($type != ".docx") {
            echo ("请上传docx格式文件！") . '<br>';
        } else {
            if ($fileinfo['size'] < 2097152 && $fileinfo['size'] > 0) {

                $name = $fileinfo['name'];
                $name = iconv("UTF-8", "gb2312", $name);
                $path2 = "../uptaskfile/" . $_FILES['upassfile']['name'];
                $path = "../uptaskfile/" . $name;
                if (move_uploaded_file($fileinfo['tmp_name'], $path)) {
                    $updatesql = "update project set Assignment='$path2' WHERE ProjectId=$pid";
                    if ($conn->query($updatesql)) {
                        echo('上传成功!');
                    } else {
                        echo('上传失败');
                    }
                }
                if (is_dir("../uptaskfile/")) {
                    $dir = scandir("../uptaskfile/");
                    foreach ($dir as $value) {
                        echo $value;
                    }
                } else {
                    echo "目录路径错误";
                }
            } else {
                echo "请传输文件小于2M的文件";
            }
        }
    } else echo "未发现文件" . "<br>";
} else {
//    echo "没有上传答辩稿";
}
//上传答辩稿
if ($draft != null) {
    if (!empty($_FILES['uptaskfile']['name'])) {
        $fileinfo = $_FILES['uptaskfile'];
        $type = strstr($fileinfo['name'], ".");
        if ($type == ".docx"||$type == ".doc") {
            echo ("请上传docx格式文件！") . '<br>';
        } else {
            if ($fileinfo['size'] < 2097152 && $fileinfo['size'] > 0) {

                $name = $fileinfo['name'];
                $name = iconv("UTF-8", "gb2312", $name);
                $path2 = "../updraftfile/" . $_FILES['uptaskfile']['name'];
                $path = "../updraftfile/" . $name;
                if (move_uploaded_file($fileinfo['tmp_name'], $path)) {
                    $updatesql = "update project set Draft='$path2' WHERE ProjectId=$pid";
                    if ($conn->query($updatesql)) {
                        echo('上传成功!');
                    } else {
                        echo('上传失败');
                    }
                }
                if (is_dir("../uptaskfile/")) {
                    $dir = scandir("../uptaskfile/");
                    foreach ($dir as $value) {
                        echo $value;
                    }
                } else {
                    echo "目录路径错误";
                }
            } else {
                echo "请传输文件小于2M的文件";
            }
        }
    }
} else {
//    echo "没有上传答辩稿";
}



?>